package com.liuguang.common.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.liuguang.common.constans.CommonConstans;
import com.liuguang.common.enums.CommonResultEnum;
import com.liuguang.common.pojo.CommonResult;
import org.apache.commons.lang3.StringUtils;

/**
 * @ClassName: SmsUtil
 * @Description：
 * @Author: wanghr
 * Date: 3:58 PM 2020/9/18
 * Version: 1.0
 */
public class SmsUtil {

    public static CommonResult<String> sendSms(String phoneNumbers, String code) {
        if (StringUtils.isAnyEmpty(phoneNumbers, code)) {
            return CommonResult.fail(CommonConstans.PARAM_NULL);
        }
        String signName = "健康预约";
        String templateCode = "SMS_175582496";
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "LTAI4GFxF9DBLUn2fnrDUQhJ", "lwTJarO3wbBxe88hPYoK3IYiXZB00i");
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        request.setSysMethod(MethodType.POST);
        request.setSysDomain("dysmsapi.aliyuncs.com");
        request.setSysVersion("2017-05-25");
        request.setSysAction("SendSms");
        request.putQueryParameter("RegionId", "cn-hangzhou");
        request.putQueryParameter("PhoneNumbers", phoneNumbers);
        request.putQueryParameter("SignName", signName);
        request.putQueryParameter("TemplateCode", templateCode);
        request.putQueryParameter("TemplateParam", "{\"code\":\"" + code + "\"}");
        try {
            CommonResponse response = client.getCommonResponse(request);
            String responseData = response.getData();
            System.out.println(responseData);
            JSONObject responseObject = JSONObject.parseObject(responseData);
            if ("OK".equals(responseObject.getString("Message"))) {
                return CommonResult.success(CommonConstans.SMS_SEND_SUCCESS);
            }
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return CommonResult.fail(CommonConstans.SMS_SEND_FIAL);
    }

    public static void main(String[] args) {
        String phoneNumbers = "13716948814";
        String signName = "健康预约";
        String templateCode = "SMS_175582496";
//        sendSms(phoneNumbers, signName, templateCode);
    }
}
